//
//  ToastModifier.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/15.
//

import SwiftUI
import Kingfisher
import Combine
import LeanCloud

//给View新增扩展方法
extension View {
    //来信通知
    public func showWeeklyLetter(isPresent:Binding<Bool>, bean: AiRoleBean) -> some View {
        //modifier 给当前试图进行修改
        modifier(showWeeklyLetterDialogModifier(isPresent: isPresent, bean: bean))
    }
}

extension View {
    //来的打开动画
    public func showWeeklyLetterOpen(isPresent:Binding<Bool>, isPlaying:Binding<Bool>, bean: WeeklyLetterBean) -> some View {
        //modifier 给当前试图进行修改
        modifier(showWeeklyLetterOpenDialogModifier(isPresent: isPresent, isPlaying: isPlaying, bean: bean))
    }
    
}


/**
 * 试图修改器
 * VoiceWaitDialog
 */
struct showWeeklyLetterDialogModifier: ViewModifier {
    @Binding var isPresent: Bool
    var bean: AiRoleBean
    
    func body(content: Content) -> some View {
        ZStack {
            content
            if isPresent {
                ZStack {
                    WeeklyLetterDialog(bean: bean)
                }.frame(maxHeight: .infinity,alignment: .top)
                    .transition(.move(edge: .top))
                    .animation(.easeInOut(duration: 2), value: isPresent)
            }
        }
    }
    
}

/**
 * 试图修改器
 * VoiceWaitDialog
 */
struct showWeeklyLetterOpenDialogModifier: ViewModifier {
    @Binding var isPresent: Bool
    @Binding var isPlaying: Bool
    @State var opened: Bool = false
    var bean: WeeklyLetterBean
    
    func body(content: Content) -> some View {
        ZStack() {
            content.blur(radius: isPresent ? 5 : 0) // 设置模糊半径为 5
            if isPresent && !opened {
                ZStack(alignment: .topTrailing) {
                    WeeklyLetterOpenDialog(bean: bean, isPlaying: $isPlaying, opened: $opened)
                    Image(.appIconClose3)
                        .resizable()
                        .frame(width: 23, height: 23, alignment: .topTrailing)
                        .padding(.trailing, 15)
                        .onTapGesture {
                            commonManager.hideWeeklyLetterOpen()
                        }
                }.background(Color.hex("#000000").opacity(0.2))
                    .transition(.opacity)
                    .animation(.easeInOut(duration: 2), value: isPresent)
            } else if opened {
                //打开信件内容
                WeeklyLetterDetailView(bean: bean) {
                    opened = false
                    commonManager.hideWeeklyLetterOpen()
                }.transition(.opacity)
                    .animation(.easeInOut(duration: 2), value: isPresent).onAppear {
                        WeeklyLetterViewModel().doRead(bean: bean)
                    }
            }
        }
    }
    
}

//来信通知
struct WeeklyLetterDialog : View {
    var bean: AiRoleBean
    
    var body: some View {
        HStack {
            VStack{
                HStack {
                    Image(.homeIconMail)
                        .resizable()
                        .renderingMode(.template)
                        .scaledToFit()
                        .foregroundColor(themeManager.themeColor)
                        .frame(width: 25, height: 25)
                    Text("每周来信")
                        .font(.system(size: 16))
                        .foregroundStyle(.bgNormalText2)
                }
                Text("给你写了一封信")
                    .font(.system(size: 14))
                    .foregroundStyle(.bgNormalText4)
            }
            Spacer()
            KFImage(URL(string: bean.avatar.formatCdn()))
                .resizable()
                .placeholder{
                    Image(.appDefaultLogo)
                        .resizable()
                        .scaledToFill()
                        .padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
                        .frame(width: 35,height: 35)
                        .clipShape(RoundedRectangle(cornerRadius: 8))
                }
                .background(Color.hex("515151").opacity(0.1))
                .scaledToFill()
                .frame(width: 35,height: 35)
                .clipShape(RoundedRectangle(cornerRadius: 8))
        }.frame(maxWidth: .infinity)
            .padding(.horizontal,15)
            .padding(.vertical,10)
            .background(.bgDialog)
            .cornerRadius(15)
            .padding(.horizontal,15)
            .contentShape(Rectangle())
            .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: 3) // 添加阴影效果
            .onAppear{
                if !AudioPlayerManager.shared.isPlaying() {
                    //播放来电声音
                    AudioPlayerManager.shared.playLocal(forResource:"notice",ofType:"mp3", looper : false)
                }
            }
            .onTapGesture {
                pathManager.jump(type: JumpType.WEEKLY_LETTER)
                commonManager.hideWeeklyLetter()
            }
            .onDisappear {
                //                AudioPlayerManager.shared.pause()//暂停播放
            }
    }
}

//来信打开
struct WeeklyLetterOpenDialog : View {
    var bean: WeeklyLetterBean
    @Binding var isPlaying: Bool
    @Binding var opened: Bool

    var body: some View {
        ZStack {
            LottieView(filename: "open_letter", loop: false, speed: 1.0, isPlaying: $isPlaying) {
                opened = true
                //打开信件内容,由于是导航栏，不知道怎么取消动画，所以改成用opened
//                commonManager.hideWeeklyLetterOpen()
//                pathManager.jump(type: JumpType.WEEKLY_LETTER_DETAIL, data: bean)
            }
            .ignoresSafeArea() // 忽略安全区域，充满整个屏幕
            
            ZStack{}
                .frame(maxWidth: .infinity, maxHeight: 200)
                .contentShape(Rectangle())
                .onTapGesture {
                    isPlaying = true
                }
        }.onChange(of: isPlaying) { newValue in
            playLocal()
        }.onAppear {
            playLocal()
        }
    }
    
    private func playLocal() {
        if isPlaying && !AudioPlayerManager.shared.isPlaying() {
            //播放打开信封,延迟0.8s
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
                AudioPlayerManager.shared.playLocal(forResource:"letter",ofType:"mp3", looper : false)
            }
        }
    }
    
}

//#Preview {
//    VoiceCallView()
//}
//定义预览提供者
struct WeeklyLetterDialog_Previews: PreviewProvider {
    
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: WeeklyLetterDialog(bean: AiRoleBean()))
    }
}

